/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-10-25
 * Time:20:36
 */
class TreeNode{
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }
}
class Solution {
    //将二叉树换成链表
    //错误 不能连接左右子树
//    public TreeNode Convert(TreeNode pRootOfTree) {
//        if(pRootOfTree==null){
//            return null;
//        }
//        TreeNode left=Convert(pRootOfTree.left);
//        TreeNode right=Convert(pRootOfTree.right);
//        if(left!=null&&right!=null){
//            left.right=pRootOfTree;
//            pRootOfTree.left=left;
//            pRootOfTree.right=right;
//            right.left=pRootOfTree;
//            return left;
//        }
//        if(left==null){
//            pRootOfTree.right=right;
//            right.left=pRootOfTree;
//            return pRootOfTree;
//        }
//        left.right=pRootOfTree;
//        pRootOfTree.left=left;
//        return left;
//    }
    TreeNode ret1=null;
    //    public TreeNode Convert(TreeNode pRootOfTree) {
//        if(pRootOfTree==null){
//            return null;
//        }
//        //进行中序遍历
//        TreeNode left=Convert(pRootOfTree.left);
//        if(left!=null){
//            left.right=pRootOfTree;
//            pRootOfTree.left=left;
//            ret=pRootOfTree;
//        }else{
//            ret=pRootOfTree;
//        }
//        TreeNode right=Convert(pRootOfTree.right);
//        if(right!=null){
//            ret.right=right;
//            right.left=ret;
//            ret=pRootOfTree;
//        }
//        while(pRootOfTree.left!=null){
//            pRootOfTree=pRootOfTree.left;
//        }
//        return pRootOfTree;
//    }
    TreeNode ret = null;

    public void  ConvertTree(TreeNode root) {
        if (root == null) {
            return;
        }
        ConvertTree(root.left);
        root.left = ret;
        if(ret!=null){
            ret.right=root;
        }
        ret = root;
        ConvertTree(root.right);
    }
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree==null){
            return null;
        }
        ConvertTree(pRootOfTree);
        TreeNode head = pRootOfTree;
        while (head.left != null) {
            head = head.left;
        }
        return head;
    }
}
public class Test {
}
